#include "Dictionary/KeyRecommander.h"
#include "Dictionary/EditDistance.h"


KeyRecommander::KeyRecommander(const string &word)
:_sought(word)
{   doQuery();
}

void KeyRecommander::doQuery()
{
    vector<pair<string,int>> candidates = Dictionary::getInstance()->doQuery(_sought);
    for(auto pr : candidates){
        //计算相关词的距离
        int dis = editDistance(_sought,pr.first);
        //int dis = editDistance("a","ab");
        //组成candidateresult
        CandidateResult candidate;
        candidate._word = pr.first;
        candidate._dist = dis;
        candidate._freq = pr.second;
        //LogDebug("排序之前：%s %d %d",candidate._word.c_str(),candidate._dist,candidate._freq);
        //放入优先级队列
        _prique.push(candidate);
    }
}

string KeyRecommander::priority_queue_pop(int num)
{
    json js;
    for(int i=0;i<num;++i){
        if(!_prique.empty()){
            js.push_back(_prique.top()._word);//将队列顶端的词放入json中
            _prique.pop();
        }
    }
    return js.dump();
}